const Joi = require('joi');

class ProcessValidate {

  async insert(param) {
    const schema = Joi.object({
      order_id: Joi.string()
        .required()
        .messages({
          'string.required': '订单id是必填项',
          'string.empty': '订单id不能为空',
        }),

      status: Joi.number()
        .required()
        .messages({
          'number.required': '状态是必填项',
          'number.empty': '状态不能为空',
        }),

      type: Joi.string()
        .required()
        .messages({
          'string.required': '类型是必填项',
          'string.empty': '类型不能为空',
        }),

      title: Joi.string()
        .required()
        .messages({
          'string.required': '标题是必填项',
          'string.empty': '标题不能为空',
        }),
    });

    try {
      const value = await schema.validateAsync(param);
      return value;
    } catch (error) {
      throw error;
    }
  }


  async stage(param) {
    const schema = Joi.object({
      order_id: Joi.string()
        .required()
        .messages({
          'string.required': '订单id是必填项',
          'string.empty': '订单id不能为空',
        }),
      order: Joi.string()
        .required()
        .messages({
          'string.required': '排序字段是必填项',
          'string.empty': '排序字段不能为空',
        }),

      method: Joi.string()
        .required()
        .messages({
          'string.required': '排序方式是必填项',
          'string.empty': '排序方式不能为空',
        }),


      pageIndex: Joi.string()
        .required()
        .messages({
          'string.required': '页数是必填项',
          'string.empty': '页数不能为空',
        }),


      pageSize: Joi.string()
        .required()
        .messages({
          'string.required': '页面大小是必填项',
          'string.empty': '页面大小不能为空',
        }),
    });


    try {
      const value = await schema.validateAsync(param);

      return value;
    } catch (error) {
      throw error;
    }
  }


  async update(param) {
    const schema = Joi.object({
      order_id: Joi.string()
        .required()
        .messages({
          'string.required': '订单id是必填项',
          'string.empty': '订单id不能为空',
        }),

      status: Joi.number()
        .required()
        .messages({
          'number.required': '状态是必填项',
          'number.empty': '状态不能为空',
        }),

      remark: Joi.string()
        .required()
        .messages({
          'string.required': '标题是必填项',
          'string.empty': '标题不能为空',
        }),
    });

    try {
      const value = await schema.validateAsync(param);

      return value;
    } catch (error) {
      throw error;
    }
  }

  async finish(param) {
    const schema = Joi.object({
      order_id: Joi.string()
        .required()
        .messages({
          'string.required': '订单id是必填项',
          'string.empty': '订单id不能为空',
        }),
      remark: Joi.string()
        .required()
        .messages({
          'string.required': '标题是必填项',
          'string.empty': '标题不能为空',
        }),
    });


    try {
      const value = await schema.validateAsync(param);

      return value;
    } catch (error) {
      throw error;
    }
  }

  async refuse(param) {
    const schema = Joi.object({
      order_id: Joi.string()
        .required()
        .messages({
          'string.required': '订单id是必填项',
          'string.empty': '订单id不能为空',
        }),
      remark: Joi.string()
        .required()
        .messages({
          'string.required': '标题是必填项',
          'string.empty': '标题不能为空',
        }),
    });

    try {
      const value = await schema.validateAsync(param);
      return value;
    } catch (error) {
      throw error;
    }
  }

}

module.exports = new ProcessValidate();
